package cn.huanzi.qch.baseadmin.sys.bookcard.controller;

import cn.huanzi.qch.baseadmin.common.controller.CommonController;
import cn.huanzi.qch.baseadmin.sys.bookcard.pojo.BookCard;
import cn.huanzi.qch.baseadmin.sys.bookcard.repository.BookCardRepository;
import cn.huanzi.qch.baseadmin.sys.bookcard.service.BookCardService;
import cn.huanzi.qch.baseadmin.sys.bookcard.vo.BookCardVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/bookCard/manage")
public class BookCardController extends CommonController<BookCardVo, BookCard, String> {
    @Autowired
    private BookCardService bookCardService;

    @Autowired
    private BookCardRepository bookCardRepository;

    @Value("${fileDir}")
    private String fileDir;

    @GetMapping("addPage")
    public ModelAndView addPage(){
        return new ModelAndView("sys/bookcard/add");
    }

    @GetMapping("examingPage")
    public ModelAndView examingPage(){
        return new ModelAndView("sys/bookcard/examing");
    }

    @GetMapping("passPage")
    public ModelAndView passPage(){
        return new ModelAndView("sys/bookcard/pass");
    }

    @PostMapping("saveBookCard")
    public void saveBookCard(@RequestBody BookCard bookCard){
        bookCardRepository.save(bookCard);
    }

    @PostMapping("listBookCard")
    public List<BookCard> listBookCard(@RequestBody BookCard bookCard){
        return bookCardRepository.findAll();
    }

    @PostMapping("/upload")
    public Map<String,Object> upload(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        String targetFilePath = fileDir;
        String fileOriginalFileName = file.getOriginalFilename(); //原始名
        String suffixName = fileOriginalFileName.substring(fileOriginalFileName.lastIndexOf(".")).toLowerCase(); //后缀名
        String fileName = UUID.randomUUID().toString().replace("-", "") + suffixName;
        String allPath = targetFilePath + File.separator + fileName;
        File targetFile = new File(allPath);
        Map<String,Object> res = new HashMap<>();
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(targetFile);
            IOUtils.copy(file.getInputStream(), fileOutputStream);
            log.info("------>>>>>>uploaded a file successfully!<<<<<<------");
            res.put("url", "/file/"+fileName);
        } catch (IOException e) {

        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                log.error("", e);
            }
        }
        return res;
    }

}